<?php

namespace App\Http\Controllers\Api\Wechat;


use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Tag;
use Illuminate\Support\Facades\Auth;

/**
 * Class BaseController
 * @package App\Http\Controllers\Api
 */
abstract class BaseController extends Controller
{
    /**
     * @var null
     */
    protected $category = null;

    /**
     * @var null
     */
    protected $tags = null;

    protected $user = null;
    /**
     * 返回数据
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     */
    protected function json($data = [], $msg = 'success', $code = 20000)
    {
/*        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];*/
        $result = array_merge($data, ['code'=>$code, 'msg'=>$msg]);
        return response()->json($result);
    }

    protected function getUser(){
        if ($this->user === null) {
            $this->user = Auth::guard('api')->user();
        }

        return $this->user;
    }
    /**
     * 获取分类
     * @return array
     */
    protected function getCategories()
    {
        if ($this->category === null) {
            $this->category = Category::getTree();
        }

        return $this->category;
    }

    /**
     * 获取标签
     * @return array|null
     */
    protected function getTags()
    {
        if ($this->tags === null) {
            $list = Tag::all(['id', 'title']);
            $this->tags = $list->toArray();
        }

        return $this->tags;
    }
}